<?php

class user extends Controller
{

	function __constructor()
	{
		parent::__constructor();
	}
	
	function index()
	{
		$this->load->view("user/home");	
	}
	
	function showallusers()
	{
		is_logged_in();
		
		$this->load->model("model_user");
		$Allusers = $this->model_user->getUsers(99999,array("user.created", "desc"), array("user.active"=>1));
		$data = array(  "crum" => build_crum( array("Admin", "Manage Users") )
						,"Allusers" => $Allusers 
					  );
		
		$this->load->view("user/showallusers", $data);	
	}	
	
	function login()
	{
		$_SESSION['user_id']=0;
		$msg = "";
		$u = $this->input->post("username");
		$p = $this->input->post("password");
		$a = $this->input->post("action");
		
		if($u != "" && $p != "" && $a == "login")
		{
			$this->load->model("model_user");
			$r = $this->model_user->isValidUser(array("username" => $u, "password" =>md5($p), "active" => 1));	
			if($r != 0)
			{
				$_SESSION['user_id'] = $r[0]['id'];
				$_SESSION['user_fullname'] = $r[0]['firstname'] ." ". $r[0]['lastname'];
				$_SESSION['user_type'] = $r[0]['user_type'];
				$_SESSION['user_company'] = $r[0]['company_id'];
				$lbl = $this->model_siteconf->get($r[0]['company_id']);
				
				$_SESSION['label1'] = $lbl[0]['conf_value'];

				$this->model_user->update_last_login($_SESSION['user_id']);
				
				if($r[0]['user_type'] == "3")
				{
					redirect("clientview");
				}
				else{
					redirect("home/");
				}
			}
			else{
				$msg = "Invalid User!";
				//redirect("user/login/");
			}
		}
		$data = array("msg" => $msg);
		$this->load->view("user/login", $data);
	}
	
	function newuser()
	{
		is_logged_in();
		$this->load->model("model_lu");
		$user_types = $this->model_lu->get("user_type");
		$uid = $this->input->post("uid");
		
		
		//===
		$this->load->model("model_company");
		$clients = $this->model_company->getAll();
		
		//===
		if( $this->input->post("action") == "newuser")
		{
			$this->load->model("model_user");
			
			$company_id = 0;
			if($this->input->post("user_type") == 3)
			{
				$company_id = $this->input->post("company");
			}
			
			$newUserData = array(
									"firstname" => $this->input->post("firstname")
									,"lastname" => $this->input->post("lastname")
									,"user_type" => $this->input->post("user_type")
									,"username" => $this->input->post("username")
									,"password" => md5($this->input->post("password"))
									,"company_id" => $company_id
								);
			$insert_id = $this->model_user->newUser($newUserData);
			
			$newUser = $this->model_user->getUserData($insert_id);
			$msg = "User " . $newUser[0]['firstname']." ".$newUser[0]['lastname']. " has been added!";
			
			$data = array(	"crum" => build_crum( array("Admin", "Manage Users", "New User") )
							,"msg" => $msg
							,"user_types" => $user_types);
		}
		else{
			
			$msg = "";
			$data = array( "crum" => build_crum( array("Admin", "Manage Users", "New User") )
							,"user_types" => $user_types
							,"msg" => $msg);
		}
		$data['clients'] = $clients;
		$this->load->view("user/newuser",$data);
	}

	
	function edituser()
	{
		is_logged_in();
		$this->load->model("model_lu");
		$user_types = $this->model_lu->get("user_type");
		
		$this->load->model("model_company");
		$clients = $this->model_company->getAll();
		
		$this->load->model("model_user");
		
		$user = $this->model_user->getUserData($this->uri->segment(3));
		$msg = "";
		
		$data = array(	"crum" => build_crum( array("Admin", "Manage Users", "Update User") )
						,"msg" => $msg
						,"user_types" => $user_types
						,"user" => $user);
		$data['clients'] = $clients;
		$this->load->view("user/edituser",$data);
	}

	function updateuser()
	{
		is_logged_in();

		if( $this->input->post("uid") != "" )
		{
			$this->load->model("model_user");
			
			$company_id = 0;
			if($this->input->post("user_type") == 3)
			{
				$company_id = $this->input->post("company");
			}
			$newUserData = array(
									"firstname" => $this->input->post("firstname")
									,"lastname" => $this->input->post("lastname")
									,"user_type" => $this->input->post("user_type")
									,"username" => $this->input->post("username")
									,"company_id" => $company_id
								);
								
			if($this->input->post("password") != "")
			{
				$newUserData['password'] = md5($this->input->post("password"));
			}
			$this->model_user->updateUser($newUserData,$this->input->post("uid") );
		}
		redirect(base_url()."user/showallusers");
		
		//$this->load->view("user/newuser",$data);
	}
	
	
	function logout()
	{
		unset($_SESSION);
		session_destroy();
		redirect(base_url());
		
	}
	function find()
	{
		is_logged_in();
		$str = $this->input->post('search');
		$r = $this->service_search->simpleSearch("user", array("name" => $str));
		
		$data = array(	"crum" => build_crum( array("User", "Search") )
					  	,"search" => $r
						,"msg" => "searching for $str");
		
		$this->load->view("user/search", $data);
	}	
	
}